//ui.cpp
#include "ui.h"

ui::ui ()
{
	cout << endl << endl << "PERSONAL ENCRYPT/DECRYPT" << endl;
	cout << "A very simple encryption and decryption program" << endl << endl << endl;
	cout << "by 13510016@std.informatika.org" << endl << endl;
	cout << "WELCOME" << endl;
	cout << "====================================================" << endl;
	bool exit = false;
	int command;
	while (!exit)
	{
		cout << ">> ";
		command = readcommand();
		switch (command)
		{
			case 0 : {subhelp(); break;}
			case 1 : {subenc(); break;}
			case 2 : {subdec(); break;}
			case -9 : {exit = true; break;}
			default : {cout << "Command is not recognized" << endl << "Type help for list of commands" << endl; break;}
		}
		cout << endl;
	}
	cout << "Thank you for trying!" << endl << endl;
}

int ui::readcommand()
{
	char* in = new char[10];
	cin >> in;
	word com(in);
	delete [] in;
	char* cstr = com.getstr();
	if ((com.getlen() == 4) && (cstr[0] == 'h') && (cstr[1] == 'e') && (cstr[2] == 'l') && (cstr[3] == 'p'))
		return 0;
	else if ((com.getlen() == 7) && (cstr[0] == 'e') && (cstr[1] == 'n') && (cstr[2] == 'c') && (cstr[3] == 'r') && (cstr[4] == 'y') && (cstr[5] == 'p') && (cstr[6] == 't'))
		return 1;
	else if ((com.getlen() == 7) && (cstr[0] == 'd') && (cstr[1] == 'e') && (cstr[2] == 'c') && (cstr[3] == 'r') && (cstr[4] == 'y') && (cstr[5] == 'p') && (cstr[6] == 't'))
		return 2;
	else if ((com.getlen() == 4) && (cstr[0] == 'e') && (cstr[1] == 'x') && (cstr[2] == 'i') && (cstr[3] == 't'))
		return -9;
	else if ((com.getlen() == 4) && (cstr[0] == 't') && (cstr[1] == 'e') && (cstr[2] == 'x') && (cstr[3] == 't'))
		return 11;
	else
		return -99;
}

void ui::subhelp ()
{
	cout << endl << "About section" << endl << endl;
	cout << " This program is used to generate encrypted text" << endl << "  and decrypt the generated code." << endl;
	cout << endl << "Help section" << endl << endl;
	cout << " Command list :" << endl;
	cout << " \t Mode\t\t Commands\t Description" << endl;
	cout << " \tmain menu\thelp\t\tshow about and help" << endl;
	cout << " \t\t\tencrypt\t\tenter encrypt mode" << endl;
	cout << " \t\t\tdecrypt\t\tenter decrypt mode" << endl;
	cout << " \t\t\texit\t\texit program" << endl;
	cout << " \tencrypt mode\ttext\t\tenter text in defined format below" << endl;
	cout << " \t\t\texit\t\tback to main menu" << endl;
	cout << " \tdecrypt mode\ttext\t\tenter text in defined format below" << endl;
	cout << " \t\t\texit\t\tback to main menu" << endl;
	cout << " Text format :" << endl;
	cout << " \tValid input examples :" << endl;
	cout << " \t\t\t\t\tThis_is_a_valid_example." << endl;
	cout << " \t\t\t\t\teverything_must_end_with_period." << endl;
	cout << " \t\t\t\t\tinput_is_CASE_SENSITIVE." << endl;
	cout << " \t\t\t\t\tahXdmo_ew_ijgnclx." << endl;
	cout << " \t\t\t\t\tThis_is_decrypted." << endl;
	cout << " \t\t\t\t\tYou_can_input_numbers_13510016." << endl;
	cout << " \t\t\t\t\tother_characters_are_allowed_:<>?." << endl;
	cout << " \tInvalid input examples :" << endl;
	cout << " \t\t\t\t\tThis_is_an_invalid_example" << endl;
	cout << " \t\t\t\t\tdon't use spaces." << endl;
}

void ui::subenc ()
{
	bool exit = false;
	int command;
	while (!exit)
	{
		cout << endl << "encrypt >> ";
		command = readcommand();
		switch (command)
		{
			case (-9) : {exit = true; break;}
			case (11) : {enc(); break;}
			default : {cout << "Command is not recognized" << endl; break;}
		}
	}
}

void ui::subdec ()
{
	bool exit = false;
	int command;
	while (!exit)
	{
		cout << endl << "decrypt >> ";
		command = readcommand();
		switch (command)
		{
			case (-9) : {exit = true; break;}
			case (11) : {dec(); break;}
			default : {cout << "Command is not recognized" << endl; break;}
		}
	}
}

void ui::enc ()
{
	encryptor e;
	cout << "Enter text to encrypt (max 250 char)" << endl;
	char* txt = new char[250];
	cin >> txt;
	int i = 0;
	bool valid = false;
	while ((!valid) && (i < 250))
	{
		if (txt[i] == '.')
			valid = true;
		else
			i++;
	}
	if (!valid)
		cout << "Invalid input" << endl;
	else
	{
		i = 0;
		cout << endl << "Encrypted text : ";
		while (txt[i] != '.')
		{
			if (txt[i] != '_')
			{
				char* t = new char[30];
				int j = 0;
				while ((txt[i] != '_') && (txt[i] != '.'))
				{
					t[j] = txt[i];
					i++;
					j++;
				}
				t[j] = '\0';
				word w(t);
				word encw = e.encrypt(w);
				encw.print();
				delete [] t;
				if (txt[i] == '_')
					cout << '_';
			}
			else
				i++;
		}
		cout << '.' << endl;
	}
	delete [] txt;
}

void ui::dec ()
{
	decryptor d;
	cout << "Enter text to decrypt (max 250 char)" << endl;
	char* txt = new char[250];
	cin >> txt;
	int i = 0;
	bool valid = false;
	while ((!valid) && (i < 250))
	{
		if (txt[i] == '.')
			valid = true;
		else
			i++;
	}
	if (!valid)
		cout << "Invalid input" << endl;
	else
	{
		i = 0;
		cout << endl << "Decrypted text : ";
		while (txt[i] != '.')
		{
			if (txt[i] != '_')
			{
				char* t = new char[30];
				int j = 0;
				while ((txt[i] != '_') && (txt[i] != '.'))
				{
					t[j] = txt[i];
					i++;
					j++;
				}
				t[j] = '\0';
				word w(t);
				word decw = d.decrypt(w);
				decw.print();
				delete [] t;
				if (txt[i] == '_')
					cout << '_';
			}
			else
				i++;
		}
		cout << '.' << endl;
	}
	delete [] txt;
}

